#include "hbmp.h"
//---------------------------------------------------------------------------

HBitmap::HBitmap(int w, int h)
{
    hBMP = 0L;

    bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;
    bi.bmiHeader.biCompression = BI_RGB;
    bi.bmiHeader.biSizeImage = 0;
    bi.bmiHeader.biXPelsPerMeter = 0;
    bi.bmiHeader.biYPelsPerMeter = 0;
    bi.bmiHeader.biClrUsed = 0;
    bi.bmiHeader.biClrImportant = 0;

    setSize(w, h);
}

HBitmap::~HBitmap()
{
    clear();
}

void HBitmap::setSize(int w, int h)
{
    clear();

//	width = w;
//	height = h;

    if (!(w && h))
            return;

    dc = CreateCompatibleDC(0);

    bi.bmiHeader.biWidth = w;
    bi.bmiHeader.biHeight = h;

    hBMP = CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &bits, 0, 0);
    SelectObject(dc, hBMP);
}

void HBitmap::clear()
{
    if (hBMP)
    {
        DeleteObject(hBMP);
        DeleteDC(dc);
    }
}

SColor* HBitmap::getScanLine(int y)
{
    return reinterpret_cast<SColor*>(bits) + (bi.bmiHeader.biHeight - y-1)*bi.bmiHeader.biWidth;
}

SColor& HBitmap::Pixel(int x, int y)
{
    return reinterpret_cast<SColor*>(bits)[(bi.bmiHeader.biHeight - y-1)*bi.bmiHeader.biWidth + x];
}

HDC HBitmap::getDC()
{
    return dc;
}

